<?php

use think\facade\Db;
use think\migration\Migrator;

class DistributionTables extends Migrator
{
    public function up()
    {
        // 创建分销员表
        $table = $this->table('sh_distribution_distributor');
        $table->addColumn('customer_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '客户ID'])
            ->addColumn('level', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '分销等级 1-一级 2-二级'])
            ->addColumn('total_commission', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '累计佣金'])
            ->addColumn('withdrawable_commission', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '可提现佣金'])
            ->addColumn('frozen_commission', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '冻结佣金'])
            ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '状态 0-禁用 1-启用'])
            ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
            ->addIndex(['customer_id'])
            ->addIndex(['status'])
            ->create();

        // 创建分销佣金记录表
        $table = $this->table('sh_distribution_commission');
        $table->addColumn('customer_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '分销员客户ID'])
            ->addColumn('from_customer_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '来源客户ID'])
            ->addColumn('order_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '订单ID'])
            ->addColumn('order_no', 'string', ['limit' => 50, 'default' => '', 'comment' => '订单号'])
            ->addColumn('order_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '订单金额'])
            ->addColumn('commission_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '佣金金额'])
            ->addColumn('level', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '分销层级 1-一级 2-二级'])
            ->addColumn('status', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '状态 0-待结算 1-已结算 2-已提现'])
            ->addColumn('settle_time', 'datetime', ['null' => true, 'comment' => '结算时间'])
            ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
            ->addIndex(['customer_id'])
            ->addIndex(['from_customer_id'])
            ->addIndex(['order_id'])
            ->addIndex(['status'])
            ->create();

        // 创建分销提现申请表
        $table = $this->table('sh_distribution_withdrawal');
        $table->addColumn('customer_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '分销员客户ID'])
            ->addColumn('amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '提现金额'])
            ->addColumn('fee', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '手续费'])
            ->addColumn('actual_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '实际到账金额'])
            ->addColumn('method', 'string', ['limit' => 20, 'default' => '', 'comment' => '提现方式 wechat-微信 alipay-支付宝 bank-银行卡'])
            ->addColumn('account_info', 'text', ['null' => true, 'comment' => '账号信息'])
            ->addColumn('remark', 'text', ['null' => true, 'comment' => '备注'])
            ->addColumn('status', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '状态 0-待审核 1-已通过 2-已拒绝 3-已打款'])
            ->addColumn('process_time', 'datetime', ['null' => true, 'comment' => '处理时间'])
            ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '申请时间'])
            ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
            ->addIndex(['customer_id'])
            ->addIndex(['status'])
            ->create();

        // 创建分销关系表
        $table = $this->table('sh_distribution_relation');
        $table->addColumn('customer_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '客户ID'])
            ->addColumn('distributor_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '分销员ID'])
            ->addColumn('level', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '分销层级 1-一级 2-二级'])
            ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
            ->addIndex(['customer_id'])
            ->addIndex(['distributor_id'])
            ->addIndex(['level'])
            ->create();

        // 创建分销订单表
        $table = $this->table('sh_distribution_order');
        $table->addColumn('order_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '订单ID'])
            ->addColumn('distributor_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '分销员ID'])
            ->addColumn('level', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '分销层级 1-一级 2-二级'])
            ->addColumn('commission_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '佣金金额'])
            ->addColumn('status', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '状态 0-待结算 1-已结算'])
            ->addColumn('settle_time', 'datetime', ['null' => true, 'comment' => '结算时间'])
            ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
            ->addIndex(['order_id'])
            ->addIndex(['distributor_id'])
            ->addIndex(['level'])
            ->addIndex(['status'])
            ->create();

        // 创建分销设置表
        $table = $this->table('sh_distribution_setting');
        $table->addColumn('code', 'string', ['limit' => 50, 'default' => '', 'comment' => '配置代码'])
            ->addColumn('key', 'string', ['limit' => 50, 'default' => '', 'comment' => '配置键名'])
            ->addColumn('value', 'text', ['null' => true, 'comment' => '配置值'])
            ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
            ->addIndex(['code'])
            ->addIndex(['key'])
            ->create();

        // 初始化分销设置
        $settings = [
            ['code' => 'distribution', 'key' => 'enabled', 'value' => '1'],
            ['code' => 'distribution', 'key' => 'mode', 'value' => 'everyone'],
            ['code' => 'distribution', 'key' => 'threshold_amount', 'value' => '0'],
            ['code' => 'distribution', 'key' => 'levels', 'value' => '2'],
            ['code' => 'distribution', 'key' => 'level1_commission', 'value' => '10'],
            ['code' => 'distribution', 'key' => 'level2_commission', 'value' => '5'],
            ['code' => 'distribution', 'key' => 'self_purchase', 'value' => '1'],
            ['code' => 'distribution', 'key' => 'withdrawal_fee', 'value' => '0'],
            ['code' => 'distribution', 'key' => 'min_withdrawal', 'value' => '100'],
        ];

        foreach ($settings as $setting) {
            Db::name('sh_distribution_setting')->insert($setting);
        }
    }

    public function down()
    {
        $this->table('sh_distribution_distributor')->drop();
        $this->table('sh_distribution_commission')->drop();
        $this->table('sh_distribution_withdrawal')->drop();
        $this->table('sh_distribution_setting')->drop();
        $this->table('sh_distribution_relation')->drop();
        $this->table('sh_distribution_order')->drop();
    }
}